package BuyerAgent;

import jade.content.lang.Codec;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import jade.core.Location;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.QueryPlatformLocationsAction;
import jade.domain.mobility.MobilityOntology;
import jade.lang.acl.ACLMessage;
import jade.proto.SimpleAchieveREInitiator;

/**
 * This class is the behavior of buyer agent that locate the location of main
 * container
 * 
 * @author DUY HAN PHAN
 * 
 */
public class GetMainContainerLocationBehaviour extends SimpleAchieveREInitiator {
	private ACLMessage request;
	private Buyer buyerAgent;

	/**
	 * @param buyerAgent
	 */
	public GetMainContainerLocationBehaviour(Buyer buyerAgent) {
		super(buyerAgent, new ACLMessage(ACLMessage.REQUEST));
		this.buyerAgent = buyerAgent;

		request = (ACLMessage) getDataStore().get(REQUEST_KEY);
		// fills all parameters of the request ACLMessage
		request.clearAllReceiver();
		request.addReceiver(buyerAgent.getAMS());
		request.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
		request.setOntology(MobilityOntology.NAME);
		request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);

		// creates the content of the ACLMessage
		try {
			Action action = new Action();
			action.setActor(buyerAgent.getAMS());
			action.setAction(new QueryPlatformLocationsAction());
			buyerAgent.getContentManager().fillContent(request, action);
		} catch (Exception fe) {
			fe.printStackTrace();
		}
		reset(request);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jade.proto.SimpleAchieveREInitiator#handleNotUnderstood(jade.lang.acl
	 * .ACLMessage)
	 */
	@Override
	protected void handleNotUnderstood(ACLMessage reply) {
		System.out.println(myAgent.getLocalName() + "handleNotUnderstood: " + reply.toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jade.proto.SimpleAchieveREInitiator#handleRefuse(jade.lang.acl.ACLMessage
	 * )
	 */
	@Override
	protected void handleRefuse(ACLMessage reply) {
		System.out.println(myAgent.getLocalName() + "handleRefuse: " + reply.toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jade.proto.SimpleAchieveREInitiator#handleFailure(jade.lang.acl.ACLMessage
	 * )
	 */
	@Override
	protected void handleFailure(ACLMessage reply) {
		System.out.println(myAgent.getLocalName() + "handle failure: " + reply.toString());
	}

	@Override
	protected void handleAgree(ACLMessage reply) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jade.proto.SimpleAchieveREInitiator#handleInform(jade.lang.acl.ACLMessage
	 * )
	 */
	@Override
	protected void handleInform(ACLMessage inform) {
		try {
			Buyer buyerAgentInstance = (Buyer) myAgent;
			Result results = (Result) myAgent.getContentManager().extractContent(inform);
			for (int i = 0; i < results.getItems().size(); i++) {
				Location location = (Location) results.getItems().get(i);
				if (location.getName().equals("Main-Container")) {
					buyerAgentInstance.mainContainerLocation = location;
					System.out.println("Get MainContainerLocation Succesfully: " + location.toString());
					break;
				}
			}
			// add the behavior which gets user input and moves accordingly
			// myOwner.addBehaviour(new InputListenerBehaviour(myOwner));
			// buyerAgentInstance.removeBehaviour(this);
		} catch (Codec.CodecException | OntologyException e) {
			e.printStackTrace();
		}
	}
}
